#include "dbgenre.h"
#include "connection.h"
#include <QSqlQuery>
#include <QString>
#include <QSqlError>
#include <QVariant>
#include <QSqlRecord>
#include <QDebug>

DbGenre::DbGenre()
{
}

bool DbGenre::create(QList<DtGenre *> &listGenre)
{
    if(listGenre.isEmpty())
        return true;
    Connection *con = Connection::getInstance();
//    WProgressBar * progress = WProgressBar::getInstance();
//    progress->setMaximumStep(listGenre.size());
//    progress->setText("Inserting genre");
    const int MaxBoucle = 999;
    if(con->isConnected())
    {
        int i = 0;
        QSqlQuery query;
        QList<DtGenre *>::const_iterator itG = listGenre.constBegin();
        QList<DtGenre *>::const_iterator itL = listGenre.constBegin();
        int nbInsert = 0;
        while(itG != listGenre.constEnd())
        {
            QString cmd = "INSERT INTO genre (name) ";
            i = 0;
            for(; itG != listGenre.constEnd() && i < MaxBoucle; itG++)
            {
//                progress->progress();
                if((*itG)->getId() == -1)
                {
                    if(i == 0)
                        cmd += " SELECT";
                    else
                        cmd += " UNION ALL SELECT";
                    QString n;
                    n.setNum(i);
                    cmd += " :n" + n;
                    i ++;
                }
            }
            if(i == 0)
                return true;
            nbInsert += i;
            if(!query.prepare(cmd))
            {
                qDebug() << query.lastQuery() << query.lastError().text();
                return false;
            }
            i = 0;
            for(; itL != listGenre.constEnd() && i < MaxBoucle; itL++)
            {
//                progress->progress();
                if((*itL)->getId() == -1)
                {
                    QString n;
                    n.setNum(i);
                    query.bindValue(":n" + n, (*itL)->getName());
                    i ++;
                }
            }
            if(!query.exec())
            {
                qWarning() << query.lastError().text();
            }
            query.finish();
        }
        QSqlQuery q("select max(id) as id from genre");
        if(q.next())
        {
            int lastId = q.value(0).toInt();

            int beforeId = lastId + 1 - nbInsert;

            for(QList<DtGenre *>::Iterator itId = listGenre.begin();
                itId != listGenre.end(); itId ++)
            {
                if((*itId)->getId() == -1)
                {
                    (*itId)->setId(beforeId);
                    beforeId++;
                }
            }
        }
//        progress->stop();
        return true;
    }
    return false;
}


bool DbGenre::destroy(const QList<DtGenre *> & listGenre)
{
    if(listGenre.isEmpty())
        return true;

    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "DELETE FROM genre WHERE id = ?";
        for(int i = 1; i != listGenre.size(); i++)
        {
            requete += " or id = ?";
        }
        int i = 0;
        QSqlQuery query;
        query.prepare(requete);
        for(QList<DtGenre *>::const_iterator it=listGenre.constBegin();
            it!=listGenre.constEnd(); it++)
        {
            query.bindValue(i, (*it)->getId());
            i++;
        }
        if(!query.exec())
        {
            qWarning() << dbGenreStr << dbGenreDestroy
                       << query.lastError().text();
            return false;
        }
    }
    return true;
}

bool DbGenre::update(const QList<DtGenre *> & listGenre)
{

    if(listGenre.isEmpty())
        return true;
    /*
    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "UPDATE FROM genre WHERE id = ? set name = ?";
        for(int i = 1; i != listGenre.size(); i++)
        {
            requete += " or id = ? set name = ?";
        }
        int i = 0;
        QSqlQuery query;
        query.prepare(requete);
        for(QList<DtGenre>::const_iterator it=listGenre.constBegin();
            it!=listGenre.constEnd(); it++)
        {
            query.bindValue(i, (*it).getId());
            query.bindValue(i + 1, (*it).getName());
            i += 2;
        }

        if(!query.exec())
        {
            Journal::print(Journal::Err, dbGenreStr, dbGenreUpdate,
                           query.lastError().text());
            return false;
        }
        return true;
    }
    return false;
    */
    return true;
}

bool DbGenre::getAll(Collection & collection)
{
    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "SELECT id, name FROM genre";
        QSqlQuery query(requete);
        QSqlRecord rec = query.record();

        int idCol = rec.indexOf("id");
        int nameCol = rec.indexOf("name");
        while(query.next())
        {
            DtGenre *gen = new DtGenre(query.value(nameCol).toString(),
                                       query.value(idCol).toInt());
            if(!collection.addGenre(gen))
                delete gen;
        }
        return true;
    }
    return false;
}

bool DbGenre::setHeart(const DtGenre &genre)
{
    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "UPDATE genre SET heart = :heart WHERE id = :id";
        QSqlQuery query;
        query.prepare(requete);
        query.bindValue(":heart", genre.getHeart() ? 1 : 0);
        query.bindValue(":id", genre.getId());
        if(!query.exec())
        {
            qWarning() << dbGenreStr << query.lastQuery()
                       << query.lastError().text();
            return false;
        }
        return true;
    }
    return false;
}
